package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.base.mapper.SyTypeMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.BsJiancyjBo;
import com.ruoyi.sycs.domain.vo.BsJiancyjVo;
import com.ruoyi.sycs.domain.BsJiancyj;
import com.ruoyi.sycs.mapper.BsJiancyjMapper;
import com.ruoyi.sycs.service.IBsJiancyjService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 检测依据设置Service业务层处理
 *
 * @author zyb
 * @date 2024-06-19
 */
@RequiredArgsConstructor
@Service
public class BsJiancyjServiceImpl implements IBsJiancyjService {

    private final BsJiancyjMapper baseMapper;
    private final SyTypeMapper syTypeMapper;

    /**
     * 查询检测依据设置
     */
    @Override
    public BsJiancyjVo queryById(Long jiancyjId){
        BsJiancyjVo vo = baseMapper.selectVoById(jiancyjId);
        vo.setSyType(syTypeMapper.selectVoById(vo.getSyTypeId()).getSyTypeName());
        return vo;
    }

    /**
     * 查询检测依据设置列表
     */
    @Override
    public TableDataInfo<BsJiancyjVo> queryPageList(BsJiancyjBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsJiancyj> lqw = buildQueryWrapper(bo);
        Page<BsJiancyjVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(v->{
            v.setSyType(syTypeMapper.selectVoById(v.getSyTypeId()).getSyTypeName());
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询检测依据设置列表
     */
    @Override
    public List<BsJiancyjVo> queryList(BsJiancyjBo bo) {
        LambdaQueryWrapper<BsJiancyj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsJiancyj> buildQueryWrapper(BsJiancyjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsJiancyj> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getJiancyjId() != null, BsJiancyj::getJiancyjId, bo.getJiancyjId());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), BsJiancyj::getJiancyj, bo.getJiancyj());
        lqw.eq(bo.getSyTypeId() != null, BsJiancyj::getSyTypeId, bo.getSyTypeId());
        return lqw;
    }

    /**
     * 新增检测依据设置
     */
    @Override
    public Boolean insertByBo(BsJiancyjBo bo) {
        BsJiancyj add = BeanUtil.toBean(bo, BsJiancyj.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setJiancyjId(add.getJiancyjId());
        }
        return flag;
    }

    /**
     * 修改检测依据设置
     */
    @Override
    public Boolean updateByBo(BsJiancyjBo bo) {
        BsJiancyj update = BeanUtil.toBean(bo, BsJiancyj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsJiancyj entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检测依据设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
